<!---

Copyright 2008:

Author: Marco van den Oever
Company: WSO
Website: http://www.webserviceonline.biz
Email: marco@webserviceonline.biz

License: 

This software is licensed under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.
For a copy of the GNU General Public License check the license folder or see <http://www.gnu.org/licenses/>.

Description:

wsoFarcryPolling, a Farcry plugin to show pollings on a Farcry based project

Build with great support from the Farcry-Dev group! (Google on Farcry-Dev)

Created: 18/10/08

Tested on CF8

--->
<cfsetting enablecfoutputonly="true" />
<cfimport taglib="/farcry/core/tags/formtools" prefix="ft" />
<cfimport taglib="/farcry/core/tags/extjs" prefix="extjs" />
<cfimport taglib="/farcry/core/tags/webskin" prefix="skin" />
<!--- get the polling data stats --->
<cfset obj = createObject('component',application.stcoapi.pollingvotes.typePath) />
<!--- set default answer_sum so calculate can be done --->
<cfset answers_sum = 0>
<cfloop index="i" from="1" to="#listLen(stobj.answers)#">
  <cfset props = {} />
  <cfif listGetAt(stobj.answers, #i#) GT 0>
    <cfset props.conditions.answer=#i# />
  </cfif>
  <cfset props.conditions.pollingobjectid=stobj.objectid />
  <cfset variables['answersum' & i] = obj.getMultipleByQuery(argumentCollection=props) />
  <cfif variables['answersum' & i].recordcount GT 0>
    <cfset variables['ansum' & i] = variables['answersum' & i].recordcount>
    <!--- get the total votes of all answers that exists --->
    <cfset answers_sum = answers_sum + variables['ansum' & i]>
  </cfif>
</cfloop>
<ft:processForm action="Save">
  <ft:processFormObjects typename="polling" />
</ft:processForm>
<!--- if the form was submitted by the Excel button, save data to Excel --->
<ft:processform action="xls">
  <!--- Import the POI tag library. --->
  <cfimport taglib="/farcry/plugins/wsoFarcryPolling/packages/custom/poi/" prefix="poi" />
  <!--- 
	Create an excel document and store binary data into 
	REQUEST variable. 
--->
  <poi:document 
	name="REQUEST.ExcelData"
	file="#ExpandPath( './polling_statistics.xls' )#"
	style="font-family: verdana ; font-size: 10pt ; color: black ; white-space: nowrap ;">
    <!--- Define style classes. --->
    <poi:classes>
      <poi:class 
			name="header" 
			style="font-family: verdana ; font-size: 10pt ; color: black ; background-color: GREY_25_PERCENT ; border-bottom: solid 3px GREY_40_PERCENT ;" 
			/>
    </poi:classes>
    <!--- Define Sheets. --->
    <poi:sheets>
      <poi:sheet 
			name="polling_statistics"
			freezerow="2"
			orientation="landscape"
			zoom="100%">
        <!--- Define global column styles. --->
        <poi:columns>
          <poi:column style="width: 150px ; text-align: center ;" />
          <poi:column style="width: 150px ; text-align: center ;" />
          <poi:column style="width: 150px ; text-align: center ;" />
          <poi:column style="width: 150px ; text-align: center ;" />
          <poi:column style="width: 150px ; text-align: center ;" />
          <poi:column style="width: 150px ; text-align: center ;" />
          <poi:column style="width: 150px ; text-align: center ;" />
        </poi:columns>
        <!--- Header row. --->
        <poi:row class="header">
          <poi:cell value="Date / Time created" />
          <poi:cell value="Question" />
          <cfloop index="i" from="1" to="#listLen(stobj.answers)#">
            <cfif listGetAt(stobj.answers, #i#) GTE 0>
              <cfset answer = listGetAt(stobj.answers, i)>
              <poi:cell value="#answer#" />
            </cfif>
          </cfloop>
          <poi:cell value="total" />
        </poi:row>
        <!--- Output the polling data --->
        <poi:row>
          <poi:cell type="date" value="#stobj.datetimecreated#" />
          <poi:cell value="#stobj.question#" />
          <cfloop index="i" from="1" to="#listLen(stobj.answers)#">
            <cfif variables['answersum' & i].recordcount GT 0>
              <poi:cell type="numeric" value="#variables['ansum' & i]#" />
              <cfelse>
              <poi:cell type="numeric" value="0" />
            </cfif>
          </cfloop>
          <poi:cell type="numeric" value="#answers_sum#" />
        </poi:row>
        <!--- Header row. --->
        <poi:row class="header">
          <poi:cell value="Date / Time created" />
          <poi:cell value="Question" />
          <cfloop index="i" from="1" to="#listLen(stobj.answers)#">
            <cfif listGetAt(stobj.answers, #i#) GTE 0>
              <cfset answer = listGetAt(stobj.answers, i)>
              <poi:cell value="#answer#" />
            </cfif>
          </cfloop>
          <poi:cell value="IP" />
        </poi:row>
        <!--- get all the pollingvotes to list in Excel --->
        <cfset stprops_votes = {} />
        <cfset stprops_votes.conditions = {} />
        <cfset votes = obj.getMultipleByQuery(argumentCollection=stprops_votes) />
        <!--- get the pollingvotes data for current polling --->
        <cfloop query="votes" startrow="1" endrow="#votes.recordcount#">
          <poi:row>
            <poi:cell type="date" value="#votes.datetimecreated#" />
            <poi:cell value="#stobj.question#" />
            <cfloop index="i" from="1" to="#listLen(stobj.answers)#">
              <cfif #votes.answer[currentrow]# EQ #i#>
                <cfset VARIABLES['ipanswer' & i] = 1>
                <cfelse>
                <cfset VARIABLES['ipanswer' & i] = 0>
              </cfif>
              <poi:cell type="numeric" value="#VARIABLES['ipanswer' & i]#" />
            </cfloop>
            <poi:cell value="#votes.userip#" />
          </poi:row>
        </cfloop>
      </poi:sheet>
    </poi:sheets>
  </poi:document>
  <!--- Tell the browser to expect an Excel file attachment. --->
  <cfheader
	name="content-disposition"
	value="attachment; filename=polling_statistics.xls"
	/>
  <!--- 
	Tell browser the length of the byte array output stream. 
	This will help the browser provide download duration to 
	the user. 
--->
  <cfheader
	name="content-length"
	value="#REQUEST.ExcelData.Size()#"
	/>
  <!--- Stream the binary data to the user. --->
  <cfcontent
	type="application/excel"
	variable="#REQUEST.ExcelData.ToByteArray()#"
	/>
</ft:processform>
<ft:processForm action="Save,Cancel" exit="true" />
<ft:form>
  <ft:object typename="#stobj.typename#" objectid="#stobj.objectid#" lFields="question,answers" r_stPrefix="prefix" legend="General Details" />
  <!--- if data exists show cfchart --->
  <cfif #answers_sum# GT 0>
    <!--- show the polling results in cfchart --->
    <cfchart format="png"
chartwidth="300" chartheight="300" showlegend="yes" show3d="yes">
    <cfchartseries type="pie">
    <cfloop index="i" from="1" to="#listLen(stobj.answers)#">
      <cfif variables['answersum' & i].recordcount GT 0>
        <cfset answer = listGetAt(stobj.answers, #i#) />
        <cfchartdata item="#answer# (#variables['ansum' & i]#)" value="#variables['ansum' & i]#">
        <cfelse>
        <cfset answer = listGetAt(stobj.answers, #i#) />
        <cfchartdata item="#answer# (0)" value="0">
      </cfif>
    </cfloop>
    <cfchartdata item="Total votes (#answers_sum#)" value="#answers_sum#">
    </cfchartseries>
    </cfchart>
  </cfif>
  
  <extjs:onReady> <cfoutput> Ext.get("#prefix#lTypenames").on('change', this.onClick, this, {
      buffer: 500,
      fn: function() { 
      renderWorkflowDefWebskins('#stobj.objectid#', Ext.get('#prefix#lTypenames').dom.value);
      }
      });
      
      function renderWorkflowDefWebskins(workflowDefID,lTypenames) {
      
      var el = Ext.get("editWebskins");
      
      if (workflowDefID != '') {
      
      el.slideOut('t', {
      easing: 'easeOut',
      duration: .5,
      remove: false,
      useDisplay: true,
      callback: function() {
      el.load({
      url: "#application.url.webtop#/facade/workflowFacade.cfc?method=renderWorkflowDefWebskins",
      scripts: true,
      autoAbort:true,
      callback: function() {
      el.slideIn('t', {
      easing: 'easeIn',
      duration: .5,
      remove: false,
      useDisplay: true
      })
      },
      params: {
      workflowDefID: workflowDefID,
      lTypenames: lTypenames
      }
      });
      }
      })
      
      
      
      }
      
      } </cfoutput> </extjs:onReady>
  <ft:farcryButtonPanel>
    <ft:farcryButton value="Save" />
    <ft:farcryButton value="Cancel" />
    <!--- if data exists show xls export button --->
    <cfif #answers_sum# GT 0>
      <ft:farcryButton value="xls" />
    </cfif>
  </ft:farcryButtonPanel>
</ft:form>
<cfsetting enablecfoutputonly="false" />
